package schedule.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UICommand;
import javax.faces.component.UIForm;
import javax.faces.event.ActionEvent;

public class PleaseWork {
	
	private UIForm form;
	private UIForm tableForm;
	private UICommand addCommand;
	
	public inputHr[] hour;
	public inputMin[] minutes;
	public String[] daysOfWeek;
	public inputWeek[] weekOfSemester; 
	public String currentStartHour = "800";
	public String currentStartMin = "0";
	public String currentEndHour = "900";
	public String currentEndMin = "0";
	public String currentDay="M";
	public String currentWeek="8/29 - 9/2";
	private inputTime addedTime;
	private List<inputHr> hrList;
	private List<inputTime> allTimes;
	private List<scheduledMeeting> meetings;

	public PleaseWork(){
		hour = new inputHr[11];
		hour[0] = new inputHr("8am", 8);
		hour[1] = new inputHr("9am", 9);
		hour[2] = new inputHr("10am", 10);
		hour[3] = new inputHr("11am", 11);
		hour[4] = new inputHr("12pm", 12);
		hour[5] = new inputHr("1pm", 1);
		hour[6] = new inputHr("2pm", 2);
		hour[7] = new inputHr("3pm", 3);
		hour[8] = new inputHr("4pm", 4);
		hour[9] = new inputHr("5pm", 5);
		hour[10] = new inputHr("6pm", 6);
		minutes = new inputMin[4];
		minutes[0] = new inputMin("0", 00);
		minutes[1] = new inputMin("15", 15);
		minutes[2] = new inputMin("30", 30);
		minutes[3] = new inputMin("45", 45);
		daysOfWeek = new String[5];
		daysOfWeek[0]="M";
		daysOfWeek[1]="T";
		daysOfWeek[2]="W";
		daysOfWeek[3]="R";
		daysOfWeek[4]="F";
		weekOfSemester = new inputWeek[15];
		weekOfSemester[0] = new inputWeek(0, "8/29 - 9/2");
		weekOfSemester[1] = new inputWeek(1, "9/5 - 9/9");
		weekOfSemester[2] = new inputWeek(2, "9/12 - 9/16");
		weekOfSemester[3] = new inputWeek(3, "9/19 - 9/23");
		weekOfSemester[4] = new inputWeek(4, "9/26 - 9/30");
		weekOfSemester[5] = new inputWeek(5, "10/3 - 10/7");
		weekOfSemester[6] = new inputWeek(6, "10/10 - 10/14");
		weekOfSemester[7] = new inputWeek(7, "10/17 - 10/21");
		weekOfSemester[8] = new inputWeek(8, "10/24 - 10/28");
		weekOfSemester[9] = new inputWeek(9, "10/31 - 11/4");
		weekOfSemester[10] = new inputWeek(10, "11/7 - 11/11");
		weekOfSemester[11] = new inputWeek(11, "11/14 - 11/18");
		weekOfSemester[12] = new inputWeek(12, "11/21 - 11/25");
		weekOfSemester[13] = new inputWeek(13, "11/28 - 12/2");
		weekOfSemester[14] = new inputWeek(14, "12/5 - 12/9");
		allTimes = new ArrayList<inputTime>();			
	}	
		
	public String addNew(){
		form.setRendered(true);
		addCommand.setRendered(true);
		return null;
	}
	public String save(){
		addedTime = new inputTime(Integer.parseInt(currentStartHour), Integer.parseInt(currentStartMin), Integer.parseInt(currentEndHour), Integer.parseInt(currentEndMin), 1, 1);
		allTimes.add(addedTime);
		System.out.println("added startHour " + currentStartHour + " and endHour " + currentEndHour + " on day " + currentDay);
		//System.out.println("First spots day is " + allTimes.get(0).currentDay + " Start Hour is " + allTimes.get(0).startHr);
		form.setRendered(false);
		addCommand.setRendered(true);
		return null;
	}
	public String cancel(){
		form.setRendered(false);
		addCommand.setRendered(true);
		return null;
	}
	public String delete(){
		allTimes.remove(addedTime);
		return null;
	}
	public String submit(){
		System.out.println("Submitting arrayList of size " + allTimes.size());
		int i =0;
		try{
			
			Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
			
			Connection connection = DriverManager.getConnection("jdbc:derby://localhost:1527/sample");

			Statement statement = connection.createStatement();
			
			String sql = "";
			while(i < allTimes.size()){
				sql += "INSERT INTO app.AdvisorTime(atAdvisorId, atStartHour, atStartMin, atEndHour, atEndMin, atDay) VALUES("+ 1 + 
						allTimes.get(i).startHr + ", " + allTimes.get(i).startMn + ", " + allTimes.get(i).endHr + ", " 
						+ allTimes.get(i).endMn + ", " + allTimes.get(i).currentDay + ", " + allTimes.get(i).currentWeek + ")";
				i++;
				statement.executeUpdate(sql);
			}
			
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
		return null;
	}
	public void displayTable(ActionEvent event){
		if (event.getComponent().getId().equalsIgnoreCase("hide")){
			tableForm.setRendered(false);
		}else{
			tableForm.setRendered(true);
		}
	}	
	
	public String calculateDayFromNum(int dayNum) {
		
		String dayLetter = "";
		
		switch(dayNum){
		case 0 :
			dayLetter = "M";
			break;
		case 1 : 
			dayLetter = "T";
			break;
		case 2 :
			dayLetter = "W";
			break;
		case 3 :
			dayLetter = "R";
			break;
		case 4 :
			dayLetter = "F";
			break;			
		}
		
		return dayLetter;
		
	}
	
	public String calculateWeekFromNum(int weekNum) {
		
		String week = "";
		
		switch(weekNum){
		case 0 :
			week = "8/29 - 9/2";
			break;
		case 1 : 
			week = "9/5 - 9/9";
			break;
		case 2 :
			week = "9/12 - 9/16";
			break;
		case 3 :
			week = "9/19 - 9/23";
			break;
		case 4 :
			week = "9/26 - 9/30";
			break;	
		case 5 :
			week = "10/3 - 10/7";
			break;
		case 6 : 
			week = "10/10 - 10/14";
			break;
		case 7 :
			week = "10/17 - 10/21";
			break;
		case 8 :
			week = "10/24 - 10/28";
			break;
		case 9 :
			week = "10/31 - 11/4";
			break;
		case 10 :
			week = "11/7 - 11/11";
			break;
		case 11 : 
			week = "11/14 - 11/18";
			break;
		case 12 :
			week = "11/21 - 11/25";
			break;
		case 13 :
			week = "12/5 - 12/9";
			break;
		
		}
	
		return week;
		
	}
	
	//Crazy number of getters and setters
	public inputHr[] getHourList() {
		return hour;
	}

	public inputMin[] getMinList() {
		return minutes;
	}
	public String[] getDaysOfWeek() {		
		return daysOfWeek;
	}
	public inputWeek[] getWeekOfSemester() {		
		return weekOfSemester;
	}
	public String getCurrentEndHour() {
		return currentEndHour;
	}

	public void setCurrentEndHour(String currentEndHour) {
		this.currentEndHour = currentEndHour;
	}

	public String getCurrentEndMin() {
		return currentEndMin;
	}

	public void setCurrentEndMin(String currentEndMin) {
		this.currentEndMin = currentEndMin;
	}
	

	public void setCurrentDay(String currentDay){
		this.currentDay= currentDay;
	}
	public String getCurrentDay(){
		return currentDay;
	}
	public void setCurrentWeek(String currentWeek){
		this.currentWeek= currentWeek;
	}
	public String getCurrentWeek(){
		return currentWeek;
	}
	public List<inputHr> getHrList(){
		return hrList;
	}
	public UIForm getForm() {
		return form;
	}

	public void setForm(UIForm form) {
		this.form = form;
	}

	public UICommand getAddCommand() {
		return addCommand;
	}

	public void setAddCommand(UICommand addCommand) {
		this.addCommand = addCommand;
	}

	public UIForm getTableForm() {
		return tableForm;
	}
	public void setTableForm(UIForm tableForm) {
		this.tableForm = tableForm;
	}

	public String getCurrentStartHour() {
		return currentStartHour;
	}

	public void setCurrentStartHour(String currentHour) {
		this.currentStartHour = currentHour;
	}
	public String getCurrentStartMin() {
		return currentStartMin;
	}

	public void setCurrentStartMin(String currentMin) {
		this.currentStartMin = currentMin;
	}

	public List<inputTime> getAllTimes() {
		return allTimes;
	}

	public void setAllTimes(List<inputTime> allTimes) {
		this.allTimes = allTimes;
	}
	public inputTime getAddedTime() {
		return addedTime;
	}

	public void setAddedTime(inputTime addedTime) {
		this.addedTime = addedTime;
	}

	
}
